package com.yanqu.road.dao.impl.voucher;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.voucher.UserVoucherData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVoucherDataDaoImpl extends TempDataObjectDao<UserVoucherData> {

    public static String getUserVoucherDataMapSql = "select * from `t_u_voucher` where `userId` = ?";

    public Map<Integer, List<UserVoucherData>> getUserVoucherDataMap(long userId) {
        Map<Integer, List<UserVoucherData>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserVoucherDataMapSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserVoucherData data = parse(rs);
                List<UserVoucherData> userVoucherDataList = map.getOrDefault(data.getType(), new ArrayList<>());
                userVoucherDataList.add(data);
                map.put(data.getType(), userVoucherDataList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertUserVoucherDataSql = "insert into `t_u_voucher` (userId,voucherId,type,code,price,expireTime) values(?,?,?,?,?,?)";

    @Override
    public boolean addData(UserVoucherData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVoucherId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getCode()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPrice()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExpireTime()));
        if (getDb().executeNoneQuery(insertUserVoucherDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserVoucherDataSql = "update `t_u_voucher` set type=?,code=?,price =?,expireTime=? where userId=? and voucherId=?";

    @Override
    public boolean updateData(UserVoucherData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getCode()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPrice()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExpireTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVoucherId()));
        if (getDb().executeNoneQuery(updateUserVoucherDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserVoucherData parseData(ResultSet rs) throws SQLException {
        UserVoucherData data = new UserVoucherData();
        data.setUserId(rs.getLong("userId"));
        data.setVoucherId(rs.getLong("voucherId"));
        data.setType(rs.getInt("type"));
        data.setCode(rs.getString("code"));
        data.setPrice(rs.getInt("price"));
        data.setExpireTime(rs.getLong("expireTime"));
        return data;
    }
}
